//==============================================================================
//
//               Copyright (C) 2004  Dick van Oudheusden
//  
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public
// License along with this library; if not, write to the Free
// Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
//==============================================================================
// 
//  $Date: 2005-01-30 08:05:48 $ $Revision: 1.4 $
//
//==============================================================================

#include "ofc/DLog.h"
#include "ofc/DPropertyTree.h"
#include "ofc/DTimer.h"
#include "ofc/DText.h"
#include "ofc/DBool.h"
#include "ofc/DInt.h"
#include "ofc/DDateTime.h"
#include "ofc/DFile.h"

#include "DTest.h"

//-Compound--------------------------------------------------------------------

void DPropertyTree_test()
{
#ifdef HAVE_DPROPERTYTREE
  DPropertyTree        *properties = [DPropertyTree new];
  
  DProperty            *group1  = nil;
  DProperty            *group2  = nil;
  DProperty            *group3  = nil;
  DProperty            *group11 = nil;
  DProperty            *group12 = nil;
  DProperty            *group13 = nil;
  // Group1
  DText                *hostname = [DText     new];
  DInt                 *timeout  = [DInt      new];
  DDateTime            *last     = [DDateTime new];
  // Group11
  DBool                *connect  = [DBool     new];
  // Group12
  DBool                *close    = [DBool     new];
  // Group13
  DBool                *sending  = [DBool     new];
  // Group2
  DBool                *doRepeat = [DBool     new];
  // Group3
  DText                *file1    = [DText     new];
  DText                *file2    = [DText     new];
  DText                *file3    = [DText     new];

  DFile                *file     = [DFile     new];
  
  STARTTEST();

  // Set the properties to a value
  group1  = [properties group :nil    :"Connection"];
  group11 = [properties group :group1 :"Types"     ];
  group12 = [properties group :group1 :"Errors"    ];
  group13 = [properties group :group1 :"Settings"  ];
  
  group2  = [properties group :nil :"Settings"];
  group3  = [properties group :nil :"Recent"];
  
  [hostname set :"localhost"];
  [timeout  set :14];
  [last     set :2004 :11 :29 :21 :9 :14];
  
  TEST([properties property :group1 :"hostname" :hostname] != nil);
  TEST([properties property :group1 :"timeout"  :timeout ] != nil);
  TEST([properties property :group1 :"last"     :last    ] != nil);
  
  [connect set :YES];
  [close   set :NO ];
  [sending set :YES];

  TEST([properties property :group11 :"connect" :connect] != nil);
  TEST([properties property :group12 :"close"   :close  ] != nil);
  TEST([properties property :group13 :"sending" :sending] != nil);
  
  [doRepeat set :YES];
  
  TEST([properties property :group2 :"repeat"   :doRepeat] != nil);
  
  [file1 set :"autoexec.bat"];
  [file2 set :"config.sys"  ];
  [file3 set :"command.com" ];
  
  TEST([properties property :group3 :"filename1" :file1] != nil);
  TEST([properties property :group3 :"filename2" :file2] != nil);
  TEST([properties property :group3 :"filename3" :file3] != nil);

  // Write the properties to the properties file
  if ([file open :"test-property.xml" :"w"])
  {
    TEST([properties write :file :"test-property"]);
    
    [file close];
  }

  // Reset the properties 
  [hostname set :""];
  [timeout  set :0];
  [last     set :1980 :1 :1 :0 :0 :0];

  [connect set :NO];
  [close   set :YES];
  [sending set :NO];
  
  [doRepeat set :NO];
  
  [file1 set :""];
  [file2 set :""  ];
  [file3 set :"" ];
  
  // Read the properties from the properties file
  if ([file open :"test-property.xml" :"r"])
  {
    TEST([properties read :file :"test-property"]);
    
    [file close];
  }

  // Check the values of the properties
  TEST([hostname ccompare :"localhost"] == 0);
  TEST([timeout  get] == 14);
  TEST([last year   ] == 2004);
  TEST([last month  ] == 11);
  TEST([last day    ] == 29);
  TEST([last hours  ] == 21);
  TEST([last minutes] == 9);
  TEST([last seconds] == 14);

  TEST([connect get] == YES);
  TEST([close   get] == NO );
  TEST([sending get] == YES);
  
  TEST([doRepeat get] == YES);

  TEST([file1 ccompare :"autoexec.bat"] == 0);
  TEST([file2 ccompare :"config.sys"  ] == 0);
  TEST([file3 ccompare :"command.com" ] == 0);
  
  STOPTEST();

  [file       free];
  [properties free];
#endif
}

